/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kaytto1;

import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Component;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.layouts.BoxLayout;

/**
 *
 * @author Joteitti
 */
public class BasicForm extends Form {

    private HomeService homeService;
    private Label nameLabel;
    private TextArea nameTextArea;
    private Label addressLabel;
    private TextArea addressTextArea;
    private Label postcodeLabel;
    private TextArea postcodeTextArea;
    private Label postalCodeLabel;
    private TextArea postalCodeTextArea;
    private Label phoneNumberLabel;
    private TextArea phoneNumberTextArea;
    private Label emailLabel;
    private TextArea emailTextArea;
    private CheckBox contactViaEmailCheckBox;
    private Label problemLabel;
    private TextArea problemTextArea;

    public BasicForm(HomeService homeService) {
        super();

        // Set home service
        this.homeService = homeService;

        // Create form layout and components
        setTitle(Text.getString(Text.TEXT_BASIC_TITLE));
        addCommandListener(homeService);
        addCommand(homeService.getContinueCommand());
        addCommand(homeService.getBackCommand());
        setTransitionOutAnimator(homeService.getTransition());
        setFocusScrolling(true);
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        addComponent(getNameLabel());
        addComponent(getNameTextArea());
        addComponent(getAddressLabel());
        addComponent(getAddressTextArea());
        addComponent(getPostcodeLabel());
        addComponent(getPostcodeTextArea());
        addComponent(getPostalCodeLabel());
        addComponent(getPostalCodeTextArea());
        addComponent(getPhoneNumberLabel());
        addComponent(getPhoneNumberTextArea());
        addComponent(getEmailLabel());
        addComponent(getEmailTextArea());
        addComponent(getContactViaEmailCheckBox());
        addComponent(getProblemLabel());
        addComponent(getProblemTextArea());
    }

    public boolean continueable() {
        // Check for valid data
        if (getNameTextArea().getText().length() == 0) {
            setFocused(getNameTextArea());
            return false;
        } else if (getAddressTextArea().getText().length() == 0) {
            setFocused(getAddressTextArea());
            return false;
        } else if (getPostcodeTextArea().getText().length() == 0) {
            setFocused(getPostcodeTextArea());
            return false;
        } else if (getPostalCodeTextArea().getText().length() == 0) {
            setFocused(getPostalCodeTextArea());
            return false;
        } else if (getPhoneNumberTextArea().getText().length() == 0) {
            setFocused(getPhoneNumberTextArea());
            return false;
        } else if (getProblemTextArea().getText().length() == 0) {
            setFocused(getProblemTextArea());
            return false;
        }

        return true;
    }

    public void clearTexts() {
        getNameTextArea().setText("");
        getAddressTextArea().setText("");
        getPostcodeTextArea().setText("");
        getPostalCodeTextArea().setText("");
        getPhoneNumberTextArea().setText("");
        getEmailTextArea().setText("");
        getContactViaEmailCheckBox().setText("");
        getProblemTextArea().setText("");
    }

    public String getName() {
        return getNameTextArea().getText();
    }

    public String getFullAddress() {
        StringBuffer fullAddress = new StringBuffer();
        fullAddress.append(getAddressTextArea().getText());
        fullAddress.append("\n");
        fullAddress.append(getPostcodeTextArea().getText());
        fullAddress.append(" ");
        fullAddress.append(getPostalCodeTextArea().getText());

        return fullAddress.toString();
    }

    public String getPhoneNumber() {
        return getPhoneNumberTextArea().getText();
    }

    public String getEmail() {
        return getEmailTextArea().getText();
    }

    public String getProblem() {
        return getProblemTextArea().getText();
    }

    private Label getNameLabel() {
        // Lazy initialization
        if(nameLabel == null) {
            nameLabel = new Label(Text.getString(Text.TEXT_BASIC_NAME));
            nameLabel.setAlignment(Component.LEFT);
        }

        return nameLabel;
    }

    private TextArea getNameTextArea() {
        // Lazy initialization
        if(nameTextArea == null) {
            nameTextArea = new TextArea();
            nameTextArea.setSingleLineTextArea(true);
            nameTextArea.setEditable(true);
            nameTextArea.setFocusable(true);
        }

        return nameTextArea;
    }

    private Label getAddressLabel() {
        // Lazy initialization
        if(addressLabel == null) {
            addressLabel = new Label(Text.getString(Text.TEXT_BASIC_ADDRESS));
            addressLabel.setAlignment(Component.LEFT);
        }

        return addressLabel;
    }

    private TextArea getAddressTextArea() {
        // Lazy initialization
        if(addressTextArea == null) {
            addressTextArea = new TextArea();
            addressTextArea.setSingleLineTextArea(true);
            addressTextArea.setEditable(true);
            addressTextArea.setFocusable(true);
        }

        return addressTextArea;
    }

    private Label getPostcodeLabel() {
        // Lazy initialization
        if(postcodeLabel == null) {
            postcodeLabel = new Label(Text.getString(Text.TEXT_BASIC_POSTCODE));
            postcodeLabel.setAlignment(Component.LEFT);
        }

        return postcodeLabel;
    }

    private TextArea getPostcodeTextArea() {
        // Lazy initialization
        if(postcodeTextArea == null) {
            postcodeTextArea = new TextArea();
            postcodeTextArea.setSingleLineTextArea(true);
            postcodeTextArea.setEditable(true);
            postcodeTextArea.setFocusable(true);
            postcodeTextArea.setConstraint(TextArea.NUMERIC);
        }

        return postcodeTextArea;
    }

    private Label getPostalCodeLabel() {
        // Lazy initialization
        if(postalCodeLabel == null) {
            postalCodeLabel = new Label(Text.getString(Text.TEXT_BASIC_POSTAL_CODE));
            postalCodeLabel.setAlignment(Component.LEFT);
        }

        return postalCodeLabel;
    }

    private TextArea getPostalCodeTextArea() {
        // Lazy initialization
        if(postalCodeTextArea == null) {
            postalCodeTextArea = new TextArea();
            postalCodeTextArea.setSingleLineTextArea(true);
            postalCodeTextArea.setEditable(true);
            postalCodeTextArea.setFocusable(true);
        }

        return postalCodeTextArea;
    }

    private Label getPhoneNumberLabel() {
        // Lazy initialization
        if(phoneNumberLabel == null) {
            phoneNumberLabel = new Label(Text.getString(Text.TEXT_BASIC_PHONE_NUMBER));
            phoneNumberLabel.setAlignment(Component.LEFT);
        }

        return phoneNumberLabel;
    }

    private TextArea getPhoneNumberTextArea() {
        // Lazy initialization
        if(phoneNumberTextArea == null) {
            phoneNumberTextArea = new TextArea();
            phoneNumberTextArea.setSingleLineTextArea(true);
            phoneNumberTextArea.setEditable(true);
            phoneNumberTextArea.setFocusable(true);
            phoneNumberTextArea.setConstraint(TextArea.PHONENUMBER);
        }

        return phoneNumberTextArea;
    }

    private Label getEmailLabel() {
        // Lazy initialization
        if(emailLabel == null) {
            emailLabel = new Label(Text.getString(Text.TEXT_BASIC_EMAIL));
            emailLabel.setAlignment(Component.LEFT);
        }

        return emailLabel;
    }

    private TextArea getEmailTextArea() {
        // Lazy initialization
        if(emailTextArea == null) {
            emailTextArea = new TextArea();
            emailTextArea.setSingleLineTextArea(true);
            emailTextArea.setEditable(true);
            emailTextArea.setFocusable(true);
            emailTextArea.setConstraint(TextArea.EMAILADDR);
        }

        return emailTextArea;
    }

    private CheckBox getContactViaEmailCheckBox() {
        // Lazy initialization
        if(contactViaEmailCheckBox == null) {
            contactViaEmailCheckBox = new CheckBox(Text.getString(Text.TEXT_BASIC_CONTACT_VIA_EMAIL));
        }

        return contactViaEmailCheckBox;
    }

    private Label getProblemLabel() {
        // Lazy initialization
        if(problemLabel == null) {
            problemLabel = new Label(Text.getString(Text.TEXT_BASIC_PROBLEM));
            problemLabel.setAlignment(Component.LEFT);
        }

        return problemLabel;
    }

    private TextArea getProblemTextArea() {
        // Lazy initialization
        if(problemTextArea == null) {
            problemTextArea = new TextArea();
            problemTextArea.setSingleLineTextArea(false);
            problemTextArea.setEditable(true);
            problemTextArea.setFocusable(true);
            problemTextArea.setRows(4);
        }

        return problemTextArea;
    }
}
